package com.mapbased.cc;

import java.io.*;
import java.util.*;

import org.dom4j.*;
import com.mapbased.wfw.*;
import com.mapbased.wfw.PageLoader.*;
import org.springframework.context.ApplicationContext;
import com.mapbased.da.FeatureMeta;
import com.mapbased.da.MetaFactory;

/**
 * <p>Title: </p>
 *
 * <p>Description: </p>
 *
 * <p>Copyright: Copyright (c) 2006</p>
 *
 * <p>Company: mapbased.com</p>
 *
 * @author chy_hs(Baily)
 * @version 1.0
 */
public class FeatureShow
    extends Component
{
    GlobalRef global;
    /**
     * the featureMeta o get data from
     */
    protected FeatureMeta datasource;

    public FeatureShow()
    {
        super();
    }

    protected ApplicationContext getApplicationContext()
    {
        return((SpringAppContext)global.getAppContext()).getApplicationContext();

    }

    protected void prepareQuery(RenderContext context)
    {}

    /**
     * a component can choose to parse an element or not.
     *
     * @param ele Element
     * @param lc PageLoader
     * @return Element or null:if wanna page loader parse the
     *   component,return the ele, else return null
     * @todo Implement this com.mapbased.wfw.Component method
     */
    public Element parseXml(Element ele,LoadContext lc)
    {
        this.global=lc.loader.getGlobal();
        this.fetchDataSource(ele,lc);
        return ele;
    }

    protected void fetchDataSource(Element ele,LoadContext loader)
    {
        Attribute a=ele.attribute("featureMeta");

        // String fm=ele.attributeValue("featureMeta");
        if(a == null)
        {
            a=ele.attribute("datasource");
        }
        if(a == null)
        {
            throw new java.lang.RuntimeException(
                "Cannot find featureMeta for FeatureTable component");
        }
        ele.remove(a);
        String fm=a.getValue();

        int dindex=fm.indexOf('.');
        String metafactory=fm.substring(0,dindex).trim();
        String fmeta=fm.substring(dindex + 1).trim();
        this.fetchDataSource(metafactory,fmeta);

    }

    protected void fetchDataSource(String metafactory,String fmeta)
    {
        MetaFactory metaFactory=
            (MetaFactory)this.getApplicationContext().getBean(metafactory);
        if(metaFactory == null)
        {
            throw new java.lang.NullPointerException(
                "Cannot find MetaFactory:" + metafactory);
        }
        this.datasource=metaFactory.get(fmeta);
        if(this.datasource == null)
        {
            throw new java.lang.NullPointerException(
                "Cannot find FeatureMeta:" + fmeta);
        }

    }

    /**
     * render
     *
     * @param context RenderContext
     * @throws IOException
     * @todo Implement this com.mapbased.wfw.Component method
     */
    public void render(RenderContext rc)
        throws IOException
    {
        rc.getRequestMap().put("ShowingFeature",
                               this.datasource.queryFirst(rc.getRequestMap()));
        this.defalutRenderBehavior(rc,this.segments);

    }

    /**
     * renderHeader
     *
     * @param context RenderContext
     * @param sb StringBuffer
     * @param m Map
     * @todo Implement this com.mapbased.wfw.Component method
     */
    public void renderHeader(RenderContext context,StringBuffer sb,Map m)
    {
    }

    public static void main(String[] args)
    {

    }
}
